package com.aloda.controller.user;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.constant.ErrorCode;
import com.aloda.dto.question.QuestionAnswerDTO;
import com.aloda.exception.BaseException;
import com.aloda.exception.WarnException;
import com.aloda.query.AnswerRecordPageDTO;
import com.aloda.query.PageQuery;
import com.aloda.result.PageResult;
import com.aloda.result.Result;
import com.aloda.service.UserAnswerService;
import com.aloda.vo.userAnswer.AnswerRecordPageVO;
import com.aloda.vo.userAnswer.UserAnswerPageVO;
import com.aloda.vo.userAnswer.UserAnswerResultVO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController("user-answer")
@RequestMapping("/user/answer")
public class UserAnswerController {

    @Resource
    private UserAnswerService userAnswerService;

    /**
     * 新增用户回答记录
     *
     * @param questionAnswerDTO 用户回答记录dto
     * @return
     */
    @PostMapping
    public Result<Long> addUserAnswer(@RequestBody QuestionAnswerDTO questionAnswerDTO) {
        log.info("新增用户回答记录:{}", questionAnswerDTO);
        if (BeanUtil.isEmpty(questionAnswerDTO)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(userAnswerService.addUserAnswer(questionAnswerDTO));
    }

    /**
     * 根据id查询回答记录
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<UserAnswerResultVO> getUserAnswerResultById(@PathVariable Long id) {
        if (id == null) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(userAnswerService.getUserAnswerResultById(id));
    }

    /**
     * 客户端分页查询答题记录
     *
     * @param answerRecordPageDTO
     * @return
     */
    @PostMapping("/page")
    public Result<PageResult<AnswerRecordPageVO>> pageQueryByUser(@RequestBody AnswerRecordPageDTO answerRecordPageDTO) {
        if (BeanUtil.isEmpty(answerRecordPageDTO)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        PageResult<AnswerRecordPageVO> pageResult = userAnswerService.pageQueryByUser(answerRecordPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 用户端删除答题记录
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Long> deleteByIds(@RequestBody List<Long> ids, @PathVariable Long id) {
        log.info("用户端删除答题记录,应用id={},ids集合为{}", id, ids);
        if (ids.isEmpty() || id == null) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(userAnswerService.deleteByIds(ids, id));
    }

    /**
     * 导出excel
     *
     * @param ids      所选择id
     * @param response response
     */
    @PostMapping("/export/{id}")
    public void exportRecordsToExcel(@PathVariable Long id, @RequestBody List<Long> ids, HttpServletResponse response) {
        log.info("用户端导出excel,应用id={},ids集合为{}", id, ids);
        if (id == null) {
            return;
        }
        userAnswerService.exportRecordsToExcel(ids, response, id);
    }

    /**
     * 分页查询用户本人的答题记录
     *
     * @param pageQuery
     * @return
     */
    @PostMapping("/user")
    public Result<PageResult<AnswerRecordPageVO>> pageQueryUserAR(@RequestBody PageQuery pageQuery) {
        if (BeanUtil.isEmpty(pageQuery)) {
            throw new WarnException(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(userAnswerService.pageQueryUserAR(pageQuery));
    }
}
